package zero.tech.games.phz.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import zero.tech.games.phz.data.consts.CphOp;
import zero.tech.games.phz.data.entity.Card;

public class CardUtil {
	
	public static class CardList {
		private final List<Card> cards=new ArrayList<Card>();

		public CardList(List<Card> cards) {
			Collections.sort(cards,new Comparator<Card>() {

				@Override
				public int compare(Card o1, Card o2) {
					return o1.getCard_id()-o2.getCard_id();
				}
			});
			this.cards.addAll(cards);
		}

		public List<Card> getCards() {
			return cards;
		}

		@Override
		public boolean equals(Object obj) {
			if(obj instanceof CardList) {
				CardList other=(CardList) obj;
				return other.getCards().toString().equals(cards.toString());
			}
			return super.equals(obj);
		}

		@Override
		public int hashCode() {
			return cards.toString().hashCode();
		}
		@Override
		public String toString() {
			return cards.toString();
		}
	}

	public static class CardListList {
		private final List<CardList> list=new ArrayList<CardList>();

		public CardListList(List<CardList> list){
			Collections.sort(list,new Comparator<CardList>() {

				@Override
				public int compare(CardList o1, CardList o2) {
					return o1.hashCode()-o2.hashCode();
				}
			});
			this.list.addAll(list);
		}
		public CardListList(List<CardList> list1, List<CardList> list2) {
			this.list.addAll(list1);
			this.list.addAll(list2);
			Collections.sort(this.list,new Comparator<CardList>() {

				@Override
				public int compare(CardList o1, CardList o2) {
					return o1.hashCode()-o2.hashCode();
				}
			});
		}
		public List<CardList> getList() {
			return list;
		}
		
		@Override
		public boolean equals(Object obj) {
			if(obj instanceof CardListList) {
				CardListList cardListList=(CardListList) obj;
				return cardListList.getList().toString().equals(this.list.toString());
			}
			return super.equals(obj);
		}
		
		@Override
		public int hashCode() {
			return this.list.toString().hashCode();
		}
		@Override
		public String toString() {
			return this.list.toString();
		}
	}
	
	public static List<Card> phz() {
		int index=1;
		List<Card> list=new ArrayList<Card>();
		for(int k=0;k<4;k++) {
			for(int i=1;i<=10;i++) {
				for(int j=1;j<=2;j++) {
					list.add(new Card(index++,j*100,i));
				}
			}
		}
		return list;
	}

	
	public static Map<Integer,Integer> numMap(List<Card> list) {
		Map<Integer,Integer> numMap=new HashMap<Integer, Integer>();
		for(Card card:list) {
			int card_id=card.getCard_id();
			if(!numMap.containsKey(card_id)) {
				numMap.put(card_id, 1);
			}else {
				int old=numMap.get(card_id);
				numMap.put(card_id,old+1);
			}
		}
		return numMap;
	}


	public static List<CardListList> checkHupai(List<Card> temp, Card mopai) {
		List<CardListList> res=new ArrayList<>();
		List<Card> cards=new ArrayList<>();
		cards.addAll(temp);
		if(cards.size()<2) {
		}else if(cards.size()==2) {
			if(cards.get(0).getCard_id()==cards.get(1).getCard_id()) {
				List<CardList> list=new ArrayList<>();
				list.add(new CardList(cards));
				CardListList cardListList = new CardListList(list);
				if(!res.contains(cardListList)) {
					res.add(cardListList);
				}
			}
		}else {
			if(mopai!=null) {
				cards.remove(mopai);
			}
			List<CardList> hh=new ArrayList<>();
			Map<Integer,Integer> nummap=numMap(cards);
			List<Integer> removes=new ArrayList<>();
			//将超过3张的移除
			for(Entry<Integer,Integer> entry:nummap.entrySet()) {
				int card_id=entry.getKey();
				int num=entry.getValue();
				if(num>=3) {
					List<Card> list=remove(cards,card_id,num);
					hh.add(new CardList(list));
					removes.add(card_id);
				}
			}
			if(!removes.isEmpty()) {
				for(Integer card_id:removes) {
					nummap.remove(card_id);
				}
			}
			
			if(mopai!=null) {
				cards.add(mopai);
			}
			nummap=numMap(cards);
			for(Entry<Integer,Integer> entry:nummap.entrySet()) {
				int card_id=entry.getKey();
				int num=entry.getValue();
				if(num>=2) {
					List<Card> tmp=new ArrayList<>();
					tmp.addAll(cards);
					List<Card> list=remove(tmp,card_id,2);
					List<List<List<Card>>> out=huDatas(tmp, true);
					if(!out.isEmpty()) {
						for(List<List<Card>> t:out) {
							List<CardList> result=new ArrayList<>();
							result.add(new CardList(list));
							for(List<Card> tt:t) {
								result.add(new CardList(tt));
							}
							CardListList cardListList = new CardListList(result,hh);
							res.add(cardListList);
						}
					}
				}
			}
			
			for(int i=0;i<cards.size();i++) {
				Card card=cards.get(i);
				List<Card> tmp=new ArrayList<>();
				tmp.addAll(cards);
				tmp.remove(card);
				tmp.add(card);
				List<List<List<Card>>> out=huDatas(tmp, false);
				if(!out.isEmpty()) {
					for(List<List<Card>> t:out) {
						List<CardList> result=new ArrayList<>();
						for(List<Card> tt:t) {
							result.add(new CardList(tt));
						}
						CardListList cardListList = new CardListList(result,hh);
						res.add(cardListList);
					}
				}
			}
		}
		return res;
	}
	
	private static boolean hu(List<Card> cards,boolean jiang) {
		if(cards.size()>3) {
			List<Card> tmp1=new ArrayList<>();
			tmp1.addAll(cards);
			Card first=tmp1.remove(0);
			List<List<Card>> tt=canBeYijuhua(tmp1,first,CphOp.HU);
			if(tt.isEmpty()) {
				return false;
			}else {
				for(List<Card> t:tt) {
					List<Card> tmp2=new ArrayList<>();
					tmp2.addAll(tmp1);
					tmp2.removeAll(t);
					boolean yijuhua=hu(tmp2,jiang);
					if(!yijuhua) {
						continue;
					}
					return true;
				}
			}
		}else if(cards.size()==3) {
			return toBeYijuhua(cards);
		}else if(cards.size()==2){
			if(!jiang&&cards.get(0).getCard_id()==cards.get(1).getCard_id()) {
				return true;
			}
		}else if(cards.size()==0) {
			if(jiang) {
				return true;
			}
		}
		return false;
	}
	
	public static List<List<List<Card>>> bipaiDatas(List<Card> cards, Card mopai) {
		List<List<List<Card>>> out=new ArrayList<>();
		List<Card> tmp1=new ArrayList<>();
		tmp1.addAll(cards);
		Card one=findCard(tmp1,mopai);
		if(one!=null) {
			tmp1.remove(one);
			List<List<Card>> tt=canBeYijuhua(tmp1, one,CphOp.CHIPAI);
			if(!tt.isEmpty()) {
				for(List<Card> t:tt) {
					List<List<Card>> res=new ArrayList<>();
					List<Card> tmp2=new ArrayList<>();
					tmp2.addAll(tmp1);
					tmp2.removeAll(t);
					List<Card> mm=new ArrayList<>();
					mm.add(one);
					mm.addAll(t);
					if(bipai(tmp2,mopai)) {
						res.add(mm);
						bipaiDatasDigui(tmp2, mopai,out,res,mm);
					}
				}
			}
		}
		return out;
	}
	
	
	private static void bipaiDatasDigui(List<Card> cards, Card one, List<List<List<Card>>> out, List<List<Card>> res, List<Card> temp) {
		if(cards.size()>3) {
			List<Card> tmp1=new ArrayList<>();
			tmp1.addAll(cards);
			Card first=findCard(tmp1,one);
			if(first!=null) {
				tmp1.remove(first);
				List<List<Card>> tt=canBeYijuhua(tmp1,first,CphOp.CHIPAI);
				if(!tt.isEmpty()) {
					for(List<Card> t:tt) {
						List<Card> tmp2=new ArrayList<>();
						tmp2.addAll(tmp1);
						tmp2.removeAll(t);
						List<Card> mm=new ArrayList<>();
						mm.add(first);
						mm.addAll(t);
						if(bipai(tmp2,one)) {
							res.add(mm);
							bipaiDatasDigui(tmp2, one,out,res,temp);
						}
					}
				}
			}else {
				List<List<Card>> tmpres=new ArrayList<>();
				tmpres.addAll(res);
				out.add(tmpres);
				res.clear();
				res.add(temp);
			}
		}else{
			if(bipai(cards, one)) {
				List<Card> tmp=new ArrayList<>();
				tmp.addAll(cards);
				tmp.add(one);
				res.add(tmp);
				List<List<Card>> tmpres=new ArrayList<>();
				tmpres.addAll(res);
				out.add(tmpres);
				res.clear();
				res.add(temp);
			}
		}
	}


	private static boolean bipai(List<Card> cards, Card mo) {
		if(cards.isEmpty()) {
			return true;
		}
		List<Card> tmp=new ArrayList<>();
		tmp.addAll(cards);
		Card one=findCard(cards,mo);
		if(one==null) {
			return true;
		}
		tmp.remove(one);
		List<List<Card>> tt=canBeYijuhua(cards, one,CphOp.CHIPAI);
		if(!tt.isEmpty()) {
			boolean ok=false;
			for(List<Card> t:tt) {
				List<Card> tmp1=new ArrayList<>();
				tmp1.addAll(tmp);
				tmp1.removeAll(t);
				ok= bipai(tmp1, one);
				if(ok) {
					break;
				}
			}
			return ok;
		}else {
			return false;
		}
	}


	public static List<List<List<Card>>> huDatas(List<Card> cards,boolean jiang) {
		List<List<List<Card>>> out=new ArrayList<>();
		List<Card> tmp1=new ArrayList<>();
		tmp1.addAll(cards);
		if(tmp1.size()>3) {
			Card first=tmp1.remove(0);
			List<List<Card>> tt=canBeYijuhua(tmp1,first,CphOp.HU);
			if(!tt.isEmpty()) {
				for(List<Card> t:tt) {
					List<List<Card>> res=new ArrayList<>();
					List<Card> tmp2=new ArrayList<>();
					tmp2.addAll(tmp1);
					tmp2.removeAll(t);
					List<Card> mm=new ArrayList<>();
					mm.add(first);
					mm.addAll(t);
					if(hu(tmp2,jiang)) {
						res.add(mm);
						huDatasDigui(tmp2, jiang,out,res,mm);
					}
				}
			}
		}else{
			if(hu(tmp1, jiang)) {
				List<List<Card>> res=new ArrayList<>();
				res.add(tmp1);
				out.add(res);
			}
		}
		return out;
	}
	
	
	
	private static void huDatasDigui(List<Card> cards, boolean jiang, List<List<List<Card>>> out, List<List<Card>> res, List<Card> temp) {
		if(cards.size()>3) {
			List<Card> tmp1=new ArrayList<>();
			tmp1.addAll(cards);
			Card first=tmp1.remove(0);
			List<List<Card>> tt=canBeYijuhua(tmp1,first,CphOp.HU);
			if(!tt.isEmpty()) {
				for(List<Card> t:tt) {
					List<Card> tmp2=new ArrayList<>();
					tmp2.addAll(tmp1);
					tmp2.removeAll(t);
					List<Card> mm=new ArrayList<>();
					mm.add(first);
					mm.addAll(t);
					if(hu(tmp2,jiang)) {
						res.add(mm);
						huDatasDigui(tmp2, jiang,out,res,temp);
					}
				}
			}
		}else{
			if(hu(cards, jiang)) {
				List<Card> tmp=new ArrayList<>();
				tmp.addAll(cards);
				res.add(tmp);
				List<List<Card>> tmpres=new ArrayList<>();
				tmpres.addAll(res);
				out.add(tmpres);
				res.clear();
				res.add(temp);
			}
		}
	}


	public static void main(String[] args) {
		List<Card> tonghua=new ArrayList<>();
//		tonghua.add(new Card(100,100, 1));
//		tonghua.add(new Card(99,100, 2));
//		tonghua.add(new Card(98,100, 3));
//		
//		tonghua.add(new Card(97,100, 4));
//		tonghua.add(new Card(96,100, 5));
//		tonghua.add(new Card(95,100, 6));
//		
//		tonghua.add(new Card(97,100, 7));
//		tonghua.add(new Card(96,100, 8));
//		tonghua.add(new Card(95,100, 9));
//		
//		
//		tonghua.add(new Card(100,100, 1));
//		tonghua.add(new Card(99,100, 2));
//		tonghua.add(new Card(98,100, 3));
//		
//		tonghua.add(new Card(97,100, 4));
//		tonghua.add(new Card(96,100, 5));
//		tonghua.add(new Card(95,100, 6));
//		
//		tonghua.add(new Card(97,100, 7));
//		tonghua.add(new Card(96,100, 8));
//		tonghua.add(new Card(95,100, 9));
//		
//		tonghua.add(new Card(100,100, 9));
//		tonghua.add(new Card(100,100, 9));
//		tonghua.add(new Card(100,200, 9));
//		
//		tonghua.add(new Card(100,100, 10));
//		tonghua.add(new Card(100,100, 10));
		boolean hu=hu(tonghua, true);
		System.out.println(hu);
		long time=System.currentTimeMillis();
		List<List<List<Card>>> out=huDatas(tonghua, false);
		System.out.println("耗时:"+(System.currentTimeMillis()-time));
		System.out.println(out.size());
		System.out.println(out);
		
		
		List<Card> tonghua1=new ArrayList<>();
		tonghua1.add(new Card(100,100, 1));
		tonghua1.add(new Card(99,100, 2));
		tonghua1.add(new Card(98,100, 3));
		
		tonghua1.add(new Card(97,100, 2));
		tonghua1.add(new Card(96,200, 2));
		
		tonghua1.add(new Card(97,100, 2));
		tonghua1.add(new Card(94,100, 3));
		tonghua1.add(new Card(93,100, 4));
		Card mo=new Card(92,100,2);
		List<List<List<Card>>> bipaiData=bipaiDatas(tonghua1,mo );
		System.out.println(bipaiData);
		
		
//		[101, 101, 206, 101, 209, 208, 109, 205, 206, 203, 109, 204, 210, 206]
		
		List<Card> tonghua2=new ArrayList<>();
		tonghua2.add(new Card(94,100, 9));
		tonghua2.add(new Card(96,100, 9));

		tonghua2.add(new Card(97,200, 8));
		tonghua2.add(new Card(96,200, 9));
		tonghua2.add(new Card(97,200, 10));
		
		tonghua2.add(new Card(98,200, 3));
		tonghua2.add(new Card(97,200, 4));
		tonghua2.add(new Card(93,200, 5));

		tonghua2.add(new Card(100,100, 1));
		tonghua2.add(new Card(99,100, 1));
		tonghua2.add(new Card(97,100, 1));
		
		tonghua2.add(new Card(98,200, 6));
		tonghua2.add(new Card(93,200, 6));
		tonghua2.add(new Card(97,200, 6));
	}




	private static List<List<Card>> canBeYijuhua(List<Card> tmp, Card first, int op) {
		List<List<Card>> list=new ArrayList<>();
		List<List<Card>> r1=toBeTonhuaShunziyijuhua(tmp,first);
		if(!r1.isEmpty()) {
			list.addAll(r1);
		}
		if(op!=CphOp.CHIPAI) {
			List<Card> r2=toBeSanzhangyijuhua(tmp,first);
			if(!r2.isEmpty()) {
				list.add(r2);
			}
		}
		List<Card> r3=toBeTonghuaErqishi(tmp,first);
		if(!r3.isEmpty()) {
			list.add(r3);
		}
		List<CardList> r4s=toBeDaxiaochi(tmp,first);
		if(!r4s.isEmpty()) {
			for(CardList cardList:r4s) {
				list.add(cardList.getCards());
			}
		}
		return list;
	}


	private static List<CardList> toBeDaxiaochi(List<Card> tmp, Card first) {
		List<CardList> result=new ArrayList<>();
		List<List<Card>> temp=new ArrayList<>();
		int num=first.getNum();
		List<Card> finds=findFromCardsByNum(tmp,num);
		Combine.combine(finds,temp, 2);
		//必须一大一小
		
		for(List<Card> cards:temp) {
			Set<Integer> set=new HashSet<>();
			set.add(first.getCard_id());
			for(Card card:cards) {
				set.add(card.getCard_id());
			}
			if(set.size()==2) {
				CardList cardList=new CardList(cards);
				if(!result.contains(cardList)) {
					result.add(cardList);
				}
			}
		}
		return result;
	}


	private static List<Card> toBeTonghuaErqishi(List<Card> tmp, Card first) {
		List<Card> result=new ArrayList<>();
		int color=first.getColor();
		int num=first.getNum();
		List<Card> tonghua=new ArrayList<>();
		for(Card card:tmp) {
			if(card.getColor()==color) {
				tonghua.add(card);
			}
		}
		if(num==2) {
			Card card7=findCardNumInTonghua(tonghua,7);
			Card card10=findCardNumInTonghua(tonghua,10);
			if(card7!=null&&card10!=null) {
				result.add(card7);
				result.add(card10);
			}
		}
		if(num==7) {
			Card card2=findCardNumInTonghua(tonghua,2);
			Card card10=findCardNumInTonghua(tonghua,10);
			if(card2!=null&&card10!=null) {
				result.add(card2);
				result.add(card10);
			}
		}
		if(num==10) {
			Card card2=findCardNumInTonghua(tonghua,2);
			Card card7=findCardNumInTonghua(tonghua,7);
			if(card2!=null&&card7!=null) {
				result.add(card2);
				result.add(card7);
			}
		}
		return result;
	}


	private static Card findCardNumInTonghua(List<Card> tonghua,int num) {
		for(Card card:tonghua) {
			if(card.getNum()==num) {
				return card;
			}
		}
		return null;
	}


	private static List<Card> toBeSanzhangyijuhua(List<Card> tmp, Card first) {
		List<Card> result=new ArrayList<>();
		int color=first.getColor();
		int num=first.getNum();
		List<Card> tonghua=new ArrayList<>();
		for(Card card:tmp) {
			if(card.getColor()==color) {
				tonghua.add(card);
			}
		}
		List<Card> finds=findFromCardsByNum(tonghua,num);
		if(finds.size()>=2) {
			result.addAll(finds.subList(0, 2));
		}
		return result;
	}


	private static List<List<Card>> toBeTonhuaShunziyijuhua(List<Card> tmp, Card first) {
		List<List<Card>> list=new ArrayList<>();
		int color=first.getColor();
		int num=first.getNum();
		List<Card> tonghua=new ArrayList<>();
		for(Card card:tmp) {
			if(card.getColor()==color) {
				tonghua.add(card);
			}
		}
		tonghua.add(first);
		//找出所有的3顺子,看是否包含了num
		List<List<Card>> res=getAllShunzi(tonghua, 3);
		for(List<Card> mp:res) {
			if(hasCardNumInList(mp,num)) {
				removeByNum(mp,first.getNum());
				list.add(mp);
			}
		}
		return list;
	}
	
	private static void removeByNum(List<Card> mp, int num) {
		for(Card card:mp) {
			if(card.getNum()==num) {
				mp.remove(card);
				return;
			}
		}
	}


	private static boolean hasCardNumInList(List<Card> mp, int num) {
		for(Card card:mp) {
			if(card.getNum()==num) {
				return true;
			}
		}
		return false;
	}


	public static List<List<Card>> getAllShunzi(List<Card> tonghua,int size){
		List<List<Card>> list=new ArrayList<>();
		List<Card> tmp = new ArrayList<>();
		tmp.addAll(tonghua);
		Collections.sort(tmp, new Comparator<Card>() {
			@Override
			public int compare(Card o1, Card o2) {
				return o1.getNum() - o2.getNum();// 按点数正序排序
			}
		});
		// 开始去重
		List<Integer> uniques = new ArrayList<>();
		List<Card> uniqueCards = new ArrayList<>();
		for (int i = 0; i < tmp.size(); i++) {
			if (!uniques.contains(tmp.get(i).getNum())) {
				uniques.add(tmp.get(i).getNum());
				uniqueCards.add(tmp.get(i));
			}
		}
		for (int i = 0; i < uniqueCards.size(); i++) {
			List<Card> out = new ArrayList<>();
			Card card0 = uniqueCards.get(i);
			out.add(card0);
			for (int j = i + 1; j < uniqueCards.size(); j++) {
				Card card1 = uniqueCards.get(j);
				if (card1.getNum() - card0.getNum() == j - i) {
					out.add(card1);
					if (out.size() == size) {
						if (!list.contains(out)) {
							List<Card> tt = new ArrayList<>();
							tt.addAll(out);
							list.add(tt);
						}
					}
				} else {
					out.clear();
					break;
				}
			}
		}
		return list;
	}



	private static boolean toBeYijuhua(List<Card> tmp) {
		boolean yijuhua=false;
		if(!yijuhua) {
			yijuhua=sanzhangyijuhua(tmp);
		}
		if(!yijuhua) {
			yijuhua=tonghuashunziyijuhua(tmp);
		}
		if(!yijuhua) {
			yijuhua=tonghuaerqishi(tmp);
		}
		if(!yijuhua) {
			yijuhua=daxiaochi(tmp);
		}
		return yijuhua;
	}


	private static boolean daxiaochi(List<Card> tmp) {
		if(tmp.size()==3) {
			int num=tmp.get(0).getNum();
			for(Card card:tmp) {
				if(card.getNum()!=num) {
					return false;
				}
			}
			return true;
		}
		return false;
	}


	public static boolean tonghuaerqishi(List<Card> tmp) {
		if(tmp.size()==3) {
			int color=tmp.get(0).getColor();
			boolean er=false;
			boolean qi=false;
			boolean shi=false;
			for(Card card:tmp) {
				if(card.getColor()!=color) {
					return false;
				}
				if(card.getNum()==2) {
					er=true;
				}
				if(card.getNum()==7) {
					qi=true;
				}
				if(card.getNum()==10) {
					shi=true;
				}
			}
			if(er&&qi&&shi) {
				return true;
			}
		}
		return false;
	}


	private static boolean tonghuashunziyijuhua(List<Card> tmp) {
		if(tmp.size()==3) {
			int color=tmp.get(0).getColor();
			Set<Integer> cardids=new HashSet<>();
			for(Card card:tmp) {
				if(card.getColor()!=color) {
					return false;
				}
				if(cardids.contains(card.getCard_id())) {
					return false;
				}
				cardids.add(card.getCard_id());
			}
			Collections.sort(tmp, new Comparator<Card>() {

				@Override
				public int compare(Card o1, Card o2) {
					return o2.getNum()-o1.getNum();
				}
			});
			if(tmp.get(0).getNum()-tmp.get(tmp.size()-1).getNum()+1==tmp.size()) {
				return true;
			}
		}
		return false;
	}


	public static boolean sanzhangyijuhua(List<Card> tmp) {
		if(tmp.size()==3&&tmp.get(0).getCard_id()==tmp.get(1).getCard_id()&&tmp.get(0).getCard_id()==tmp.get(2).getCard_id()) {
			return true;
		}
		return false;
	}


	private static List<Card> remove(List<Card> tmp, int card_id, int count) {
		List<Card> list=new ArrayList<>();
		List<Card> removes=new ArrayList<>();
		for(Card card:tmp) {
			boolean find=false;
			if(card.getCard_id()==card_id) {
				removes.add(card);
				if(removes.size()>=count) {
					find=true;
					break;
				}
			}
			if(find) {
				break;
			}
		}
		tmp.removeAll(removes);
		list.addAll(removes);
		return list;
	}


	public static List<Card> findFromHandByCardId(List<Card> hands, int card_id) {
		List<Card> list=new ArrayList<>();
		for(Card card:hands) {
			if(card.getCard_id()==card_id) {
				list.add(card);
			}
		}
		return list;
	}
	
	public static List<Card> findFromCardsByNum(List<Card> tonghua, int num) {
		List<Card> list=new ArrayList<>();
		for(Card card:tonghua) {
			if(card.getNum()==num) {
				list.add(card);
			}
		}
		return list;
	}


	public static List<List<Card>> checkChipai(List<Card> cards, Card mopai) {
		List<List<Card>> result=new ArrayList<>();
		result.addAll(canBeYijuhua(cards, mopai,2));
		return result;
	}



	private static Card findCard(List<Card> cards, Card mopai) {
		for(Card card:cards) {
			if(card.getCard_id()==mopai.getCard_id()) {
				return card;
			}
		}
		return null;
	}


	public static Card findFromCardsById(List<Card> hands, int id) {
		for(Card card:hands) {
			if(card.getId()==id) {
				return card;
			}
		}
		return null;
	}
	
	public static int findCount(List<Card> cards, Card des) {
		int count=0;
		for(Card card:cards) {
			if(card.getCard_id()==des.getCard_id()) {
				count++;
			}
		}
		return count;
	}


	public static boolean tonghuaeyiersan(List<Card> tmp) {
		if(tmp.size()==3) {
			int color=tmp.get(0).getColor();
			boolean yi=false;
			boolean er=false;
			boolean san=false;
			for(Card card:tmp) {
				if(card.getColor()!=color) {
					return false;
				}
				if(card.getNum()==1) {
					yi=true;
				}
				if(card.getNum()==2) {
					er=true;
				}
				if(card.getNum()==3) {
					san=true;
				}
			}
			if(yi&&er&&san) {
				return true;
			}
			
		}
		return false;
	}


	public static Card findOneCardFromHandByCardId(List<Card> cards, Integer card_id) {
		for(Card card:cards) {
			if(card.getCard_id()==card_id) {
				return card;
			}
		}
		return null;
	}


	public static Map<Integer, Integer> numMapByCardIds(List<Integer> cardIds) {
		Map<Integer,Integer> numMap=new HashMap<Integer, Integer>();
		for(Integer card_id:cardIds) {
			if(!numMap.containsKey(card_id)) {
				numMap.put(card_id, 1);
			}else {
				int old=numMap.get(card_id);
				numMap.put(card_id,old+1);
			}
		}
		return numMap;
	}



	public static boolean wuhu(List<Card> tmp) {
		for(Card card:tmp) {
			if(card.getNum()==2||card.getNum()==7||card.getNum()==10) {
				return false;
			}
		}
		return true;
	}


	public static boolean shisanhong(List<Card> tmp) {
		int count=0;
		for(Card card:tmp) {
			if(card.getNum()==2||card.getNum()==7||card.getNum()==10) {
				count++;
			}
		}
		if(count>=13) {
			return true;
		}
		return false;
	}


	public static boolean honghu(List<Card> tmp) {
		int count=0;
		for(Card card:tmp) {
			if(card.getNum()==2||card.getNum()==7||card.getNum()==10) {
				count++;
			}
		}
		if(count>=10&&count<13) {
			return true;
		}
		return false;
	}


	public static boolean yidianzhu(List<Card> tmp) {
		int count=0;
		for(Card card:tmp) {
			if(count>1) {
				return false;
			}
			if(card.getNum()==2||card.getNum()==7||card.getNum()==10) {
				count++;
			}
		}
		if(count==1) {
			return true;
		}
		return false;
	}
}
